"""代码高亮组件"""

import streamlit as st
from streamlit_ace import st_ace


def render_code_editor(
    value: str = "",
    language: str = "cairo",
    height: int = 300,
    key: str = "code_editor"
) -> str:
    """渲染代码编辑器"""
    return st_ace(
        value=value,
        language=language,
        height=height,
        key=key,
        theme="monokai",
        show_gutter=True,
        show_print_margin=True,
        wrap=True,
        auto_update=False,
        annotations=[],  # 可以添加错误标注
        placeholder="请输入Cairo代码...",
        font_size=14,
        tab_size=4,
        wrap_behaviour="wrap"
    )


def highlight_code_block(code: str, language: str = "cairo") -> str:
    """高亮代码块"""
    # 使用streamlit-ace的语法高亮
    return f"""
    <div class="code-highlight">
        <pre><code class="language-{language}">{code}</code></pre>
    </div>
    """


def render_code_analysis(code: str) -> None:
    """渲染代码分析结果"""
    if not code.strip():
        return
    
    st.markdown("### 📊 代码分析")
    
    col1, col2 = st.columns(2)
    
    with col1:
        st.markdown("**语法检查**")
        # 这里可以添加语法检查逻辑
        st.success("✅ 语法正确")
    
    with col2:
        st.markdown("**类型检查**")
        # 这里可以添加类型检查逻辑
        st.info("ℹ️ 需要编译验证")
    
    # 显示代码结构
    st.markdown("**代码结构**")
    if "fn " in code:
        st.markdown("- 包含函数定义")
    if "struct " in code:
        st.markdown("- 包含结构体定义")
    if "trait " in code:
        st.markdown("- 包含特征定义")
    if "impl " in code:
        st.markdown("- 包含实现块")
